package com.gec.web;

import com.gec.domain.Dept;
import com.gec.domain.Job;
import com.gec.domain.User;
import com.gec.service.DeptService;
import com.gec.service.JobService;
import com.gec.service.impl.DeptServiceImpl;
import com.gec.service.impl.JobServiceImpl;
import com.gec.utils.CrossRegion;
import com.gec.utils.PageModel;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = {"/findAllJob","/saveJob","/updateJob","/deleteJob","/findJobById","/findJob"})
public class JobController extends HttpServlet {
    //设置service
    JobService service = new JobServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //跨域设置
        CrossRegion.setCross(response);
        //如果是get,设置200ok
        if ("OPTIONS".equals(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return;
        }
        String servletPath = request.getServletPath();

        if (servletPath.equals("/findAllJob")) {
            findalljob(request,response);
        } else if (servletPath.equals("/saveJob") || servletPath.equals("/updateJob")) {
            saveorupdatejob(request,response);
        } else if (servletPath.equals("/deleteJob")) {
            deletejob(request,response);
        } else if (servletPath.equals("/findJobById")) {
            findbyidjob(request,response);
        }else if(servletPath.equals("/findJob")){
            findjob(request,response);
        }
    }

    /**
     * 添加修改
     * */
    private void saveorupdatejob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");

        String name = request.getParameter("name");
        String remark = request.getParameter("remark");

        Job job = new Job();
        job.setName(name);
        job.setRemark(remark);

        //如果id为0，则添加
        if (id.equals("0")) {
            service.addJob(job);
        } else {//反之则修改
            int uid = Integer.parseInt(id);
            job.setId(uid);
            service.updataJob(job);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("message", "success");

        Gson gson = new Gson();
        String json = gson.toJson(map);
        response.getWriter().write(json);
    }

    /**
     * 删除
     * */
    private void deletejob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        service.deletejob(Integer.parseInt(id));

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);

        Gson gson = new Gson();
        String json = gson.toJson(map);
        response.getWriter().write(json);
    }

    /**
     * 查询全部
     * */
    private void findjob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //用户列表：需要发送给前端的
        List<Job> jobs = null;

        Job job = new Job();

        //数库查询全部的结果
        jobs = service.findJob(job);

        Map<String, Object> map = new HashMap<>();
        map.put("jobs", jobs);
        //使用json返回
        Gson gson = new Gson();
        String s = gson.toJson(map);
        //响应前台
        response.getWriter().write(s);
    }

    /**
     * 查询全部，添加了分页查询
     * */
    private void findalljob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得当前页码
        String pageIndex = request.getParameter("pageIndex");
        int page = !"".equals(pageIndex) && pageIndex != null ? Integer.parseInt(pageIndex) : 1;
        //设置到PageModel
        PageModel model = new PageModel();
        model.setPageIndex(page);

        //查询用户列表
        List<Job> jobs = null;

        Job job=new Job();
        //查询用户名
        String name = request.getParameter("name");
        if (name != null) {
            System.out.println("name = " + name);
            job.setName(name);
        }

        //总记录数
        int count = service.getTotalJobCount(job);
        model.setTotalRecoreSum(count);
        jobs = service.findAllJob(job, model);

        Map<String, Object> map = new HashMap<>();
        map.put("model", model);
        map.put("jobs", jobs);

        //使用json返回
        Gson gson = new Gson();
        String s = gson.toJson(map);
        //响应前台
        response.getWriter().write(s);
    }

    /**
     * 根据id查询
     * */
    private void findbyidjob(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //从页面获取id
        String id = request.getParameter("id");
        //返回到数据库
        Job jobs = service.findJobById(Integer.parseInt(id));

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("jobs", jobs);

        Gson gson = new Gson();
        String json = gson.toJson(map);

        response.getWriter().write(json);
    }
}
